<template>
    <li class="Purchase">
        <el-form ref="form" :model="form" label-width="68px">
            <el-form-item :label="$t('jiaoyi[0]')">
                <el-input :placeholder="$t('jiaoyi[1]')" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item :label="$t('jiaoyi[2]')">
                <el-input v-model="form.buy_num" placeholder="0.00"  @focus='change'>
                   <template slot="prepend">
                        1{{$t('jiaoyi[3]')}}={{form.sheet_num + code}} 
                    </template>
                </el-input>
                <el-radio-group v-model="radio1" @change="buynumchange">
                    <el-radio-button label="0.25">25%</el-radio-button>
                    <el-radio-button label="0.5">50%</el-radio-button>
                    <el-radio-button label="0.75">75%</el-radio-button>
                    <el-radio-button label="1">100%</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('order[13]')">
                <el-select popper-class='blackSelect' v-model="form.leverage" :placeholder="$t('jiaoyi[4]')">
                    <el-option v-for="(item, index) in codetype.leverage" :key="index" :label="item" :value="item"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div class="Purchasefooter">
            <!-- {{$store.state.user.assetList.BTC}} -->
            <p>{{$t('jiaoyi[5]')}}<span>{{baozhengjin}}</span></p>
            <p>{{$t('jiaoyi[6]')}}<span>{{canUseMoney}}</span></p>
        </div>
        <el-button type="success" @click="preservation">{{$t('jiaoyi[7]')}}</el-button>
        <!-- <el-button type="success" v-if="Prodata.buy_up == 0" >{{$t('jiaoyi[7]')}}</el-button> -->
    </li>
</template>

<script>

import { CalculationTotal, CalculationPrice } from "@/libs/util";
export default {
    name: 'marketpricesell',
    props:['Prodata', "codetype","canUseMoney"],
    data () {
        return {
            form:{
                "buy_price": "",//最新价格
                "buy_num": "",//买入数量
                // "zy": "",//止盈
                // "zs": "",//止损
                "type": "1",//买入类型 1市价 2限价
                "otype": "1",//买入方向 1买涨2买跌
                "code": "",//产品名称
                "leverage": "",//杠杆
                "zhongjia": "", //总价
                "fee": "", //手续费
                "sheet_num": ""
            },
            radio1: 0,
            code:"BTC",
            baozhengjin: '',
            Data: {
                maximum: '',
            }
        }
    },
    watch: {
        'Prodata.code': function(newVal, oldVal){
            this.code = _.toUpper(_.split(newVal, "/")[0]);
            this.form.code = newVal;
        },
        "Prodata.price": function(newVal, oldVal){
                this.form.buy_price = newVal;
        },
        'codetype': function(newVal, oldVal){
            this.codetype = newVal;
        },
        'codetype.leverage': function(newVal, oldVal){
            this.form.leverage = newVal[0];
        },
        'codetype.sheet_num': function(newVal, oldVal){
            this.codetype.sheet_num = newVal;
            this.form.sheet_num = newVal;
            this.allmoney();
        },
        'canUseMoney': function(newVal, oldVal){
            this.canUseMoney = newVal;
        },
        "form.buy_price": function(newVal, oldVal){
            this.allmoney();
            this.maxCanBuy();
        },
        "form.buy_num": function(newVal, oldVal){
            if(isNaN(newVal)){
                this.form.buy_num = oldVal;
                return false;
            } else {
                if(this.form.buy_num.toString().indexOf(".") != -1){
                    let p = this.form.buy_num.toString().indexOf(".") + 1;
                    let n = Number('2')
                    this.form.buy_num = this.form.buy_num.toString().slice(0, p + n)
                }
                if (Number(this.form.buy_num) > Number(this.Data.maximum)) {
                    this.form.buy_num = Number(this.Data.maximum);
                    return false;
                }
            }
            this.allmoney();
            this.maxCanBuy();
        },
        "form.leverage": function(newVal, oldVal){
            this.form.leverage = newVal;
            this.allmoney();
            this.maxCanBuy();
        },
    },
    methods: {
        change(){
            this.radio1 = 0;
        },
        buynumchange(data){
            this.maxCanBuy();
        },
        maxCanBuy(){ //最大购买数量
            var newPrice = _.add(this.form.buy_price, _.multiply(this.codetype.spread, this.codetype.var_price));

            var  maxNum = _.divide(this.canUseMoney, _.multiply(_.multiply(this.form.sheet_num, newPrice), _.divide(1, this.form.leverage)));
            //  var  maxNum = _.divide(this.canUseMoney, _.multiply(newPrice, _.divide(1, this.form.leverage)));
        
            this.Data.maximum = _.floor(maxNum, 2);
            if(this.radio1){
                this.form.buy_num = _.floor(_.multiply(this.Data.maximum, this.radio1), 2)
            }
        },
        allmoney(){
            //市价
            var newPrice = _.add(this.form.buy_price, _.multiply(this.codetype.spread, this.codetype.var_price));

            this.baozhengjin = _.round(_.multiply(_.multiply(_.multiply(this.form.sheet_num, newPrice), this.form.buy_num), _.divide(1, this.form.leverage)), 4) ;
            // this.baozhengjin = _.round(_.multiply(_.multiply(newPrice, this.form.buy_num), _.divide(1, this.form.leverage)), 4) ;

            this.form.zhongjia = _.round(_.multiply( _.multiply(_.multiply(this.form.sheet_num, newPrice), this.form.buy_num), _.add(_.divide(1, this.form.leverage), _.toFinite(this.codetype.handling_fee))), 4) ; 
            // this.form.zhongjia = _.round(_.multiply( _.multiply(newPrice, this.form.buy_num), _.add(_.divide(1, this.form.leverage), _.toFinite(this.codetype.handling_fee))), 4) ; 
            
            this.form.fee = _.round(_.multiply(_.multiply(_.multiply(this.form.sheet_num, newPrice), this.form.buy_num), _.toFinite(this.codetype.handling_fee)), 4);
            // this.form.fee = _.round(_.multiply(_.multiply(newPrice, this.form.buy_num), _.toFinite(this.codetype.handling_fee)), 4);
        },
        preservation(){
            let Prodata = _.cloneDeep(this.form);
            if(!Prodata.buy_price){
                this.$message.error(this.$t('jiaoyi[8]'));
                return false;
            };
            if(!Prodata.buy_num){
                this.$message.error(this.$t('jiaoyi[9]'));
                return false;
            };
            if(!Prodata.leverage){
                this.$message.error(this.$t('jiaoyi[10]'));
                return false;
            };
            this.$emit('func', this.form)
        }
    },
    mounted(){
        setTimeout(()=> {
            this.form.buy_price = this.Prodata.price;
            this.form.code =  this.Prodata.code;
            this.code = _.toUpper(_.split(this.form.code, "/")[0]);
        })
    },
    components: {
        
    },
}
</script>

